package edu.zut.controller;


import edu.zut.pojo.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

//@Controller ：标明当前类是一个控制器类，它的使用权默认交给了Spring容器
@Controller
//@RequestMapping("/user") 位于类上时，表示路劲窄化， 此时当前控制器下所有的请求默认前缀都是/user
@RequestMapping("/user")
public class IndexController {

    /**
     * @RequestMapping :标注在方法上时，标明当前方式能映射的url请求，method：表示的是请求方式
     * @return 字符串类型，此时默认表示的就是当前请求要跳转的url
     * 利用request接收请求参数
     */
//    @RequestMapping(path = "/user/login",method = RequestMethod.POST)
    public String userLogin(HttpServletRequest request){
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println("name = " + name);
        System.out.println("password = " + password);
        //重定向success.html
        return "redirect:/success.html";
    }

    /**
     * Web中参数的自动绑定：请求参数的key值和处理url方法的形参名保持一致，类型兼容，自动封装
     */
//    @RequestMapping(path = "/user/login",method = RequestMethod.POST)
    public String userLogin2( String name,String password){
        System.out.println("name = " + name);
        System.out.println("password = " + password);
        //重定向success.html
        return "redirect:/success.html";
    }

    /**
     * Web中参数的自动绑定：
     * 请求参数的key值和处理url方法的形参名不一致？
     * @RequestParam 指定下形参要绑定的请求参数
     */
    @RequestMapping(path = "/login",method = RequestMethod.POST)
    public String userLogin3(@RequestParam(name = "name") String username, @RequestParam(name = "password") String psw){
        System.out.println("name = " + username);
        System.out.println("password = " + psw);
        //重定向success.html
        return "redirect:/success.html";
    }

    // @GetMapping :get请求的映射

    /**
     * 通过实体 Bean 接收请求参数
     * 要求Bean里面的属性名和请求参数名保持一致，类型兼容，set方法生成
     * Valid :用于校验Bean
     */
    @GetMapping(value = "/register")
    public String userRegister(@Valid UserInfo userInfo){
        System.out.println("userInfo = " + userInfo);
        return "/success.html";
    }

    //从路劲中解析请求参数@PathVariable
    @GetMapping("/details/{num1}/{num2}")
    public String details(@PathVariable(name = "num1") String num, @PathVariable String num2){
        System.out.println("num = " + num);
        System.out.println("num2 = " + num2);
        return "forward:/success.html";
    }

    //跳转到注册页面
    @GetMapping("/toRegister")
    public String toRegister(){
//        return "forward:/register.html";  //localhost:8080/register.html
        return "redirect:/register.html";
    }





}
